package com.mca.framework.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * @Author: zhoupeng
 * @createTime: 2025年07月11日 10:49:15
 * @version: 1.0
 * @Description:
 * @copyright:
 */
public class WhenCompleteDemo {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);

        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {

            //子任务
            try {
//                int i =1/0;
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return 123;
        }, executorService);

        //主任务
        System.out.println("main end ");



        //根据异步任务的结果，触发后续业务逻辑处理  ，并且其中不进行报错 ,是同一个线程 在获取返回后的结果在继续执行后面的逻辑
        future.whenComplete(new BiConsumer<Integer, Throwable>() {
            @Override
            public void accept(Integer integer, Throwable throwable) {
                System.out.println("异步任务后续处理线程666："+Thread.currentThread().getName());
                System.out.println(integer+123);
            }
        });
//        executorService.shutdown();
        //是不同的线程，由于传入了，线程池
//        future.whenCompleteAsync(new BiConsumer<Integer, Throwable>() {
//            @Override
//            public void accept(Integer integer, Throwable throwable) {
//
//                System.out.println("异步任务后续处理线程666："+Thread.currentThread().getName());
//                System.out.println(integer+123);
//                executorService.shutdown();
//            }
//        },executorService);

        // whenCompleteAsync 与  exceptionally 类似 但是  而exceptionally()只对异常进行处理，且其使用的是主线程
        future.exceptionally(new Function<Throwable, Integer>() {
            @Override
            public Integer apply(Throwable throwable) {

                System.out.println("异常结果处理的线程："+Thread.currentThread().getName());
                System.out.println(throwable.getMessage());
                return null;
            }
        });

        //线程报错之后才会进行执行
//        future.exceptionally((t)->{
//            System.out.println("异常结果处理的线程："+Thread.currentThread().getName()+"错误原因"+t.getMessage());
//            return null;
//        });
//         future.whenComplete((res, ex) -> {
//           if (ex != null) {
//               System.out.println("Error occurred: " + ex);
//           }else {
//               System.out.println("线程执行成功都没有报错");
//           }
//         });
    }
    /**
     * combinedFuture.whenComplete((res, ex) -> {
     *       if (ex != null) {
     *           System.out.println("Error occurred: " + ex);
     *           log.info("maTaskId：{}转换导购关系异步线程异常!",maTaskId,ex);
     *       }
     *   });
     *   combinedFuture.join(); // 此处会输出错误信息
     *   log.info("maTaskId:{}导购关系全部处理完成",maTaskId);
     */
}
